package navigation {
	import org.splink.library.utils.ArrayUtils;
	import org.splink.deepsplink.navigation.IPageNavigationController;
	import org.splink.deepsplink.navigation.IPageNavigationView;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author Max Kugland
	 */
	public class NavigationView implements IPageNavigationView {

		private var _display : DisplayObjectContainer;
		private var _controller : IPageNavigationController;
		private var _btns : Array;
		private var _excludeIds : Array = new Array;

		public function NavigationView(excludeIds : Array = null) {
			if(excludeIds) {
				_excludeIds = excludeIds;
			}
			
			_display = new Sprite();
		}

		/**
		 * @inheritDoc
		 */
		public function setController(controller : IPageNavigationController) : void {
			_controller = controller;
		}

		/**
		 * @inheritDoc
		 */
		public function render(subpageIds : Array) : void {
			_btns = new Array;
			
			var nextX : int = 0;
			for each (var id : String in subpageIds) {
				if(!ArrayUtils.contains(_excludeIds, id)) {
					var btn : Btn = new Btn(id, id);
					btn.addEventListener(MouseEvent.CLICK, onClick);
					_display.addChild(btn.display).x = nextX;
					nextX = btn.display.x + btn.display.width + 5;
					_btns.push(btn);
				}
			}
		}

		private function onClick(event : MouseEvent) : void {
			_controller.gotoPage(Btn(event.target).id);
		}

		/**
		 * @inheritDoc
		 */
		public function select(id : String, pageParams : Array = null, changed : Boolean = true) : void {
			for each (var btn : Btn in _btns) {
				if(btn.id == id) {
					btn.mark();
				} else btn.unmark();
			}
		}

		/**
		 * @inheritDoc
		 */
		public function finalize() : void {
			_btns.splice(0, _btns.length);
		}

		/**
		 * @inheritDoc
		 */
		public function get display() : DisplayObject {
			return _display;
		}
	}
}
